package 链表;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/5/6 20:34
 * @Description:
 */
public class 相交链表 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        ListNode p1 = headA;
        ListNode p2 = headB;

        while (p1 != p2){
            p1 = p1.next != null ? p1.next : headB;
            p2 = p2.next != null ? p2.next: headA;
        }
        return p1;
    }



    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}

